<template>
	<view class="my">
		<view class="bg">
			<view class="top">
			</view>
			<view class="user-list">
				<view class="user-list-top">
					<view class="user-avatar">
						<image src="../../static/avatar.png" mode="widthFix"></image>
					</view>
					<view class="user-id">
						<view class="user-name" v-if="isLogin">
							書友 {{userInfo.nickName}}
						</view>
						<view class="id" v-if="isLogin">
							手机号：{{userInfo.phone}}
						</view>
						<view class="goLogin" @click="goLogin()" v-if="!isLogin">
							去登录
						</view>
					</view>
					<view class="user-info" @click="goPersonal()">
						<image src="../../static/right.png" mode="widthFix"></image>
					</view>
				</view>
				<view class="my-state">
					<view class="my-state-list" v-for="(item,index) in myInfo" :key="index">
						<view class="count">
							{{item.count}}
						</view>
						<view class="title">
							{{item.title}}
						</view>
					</view>
					<view class="sign" v-if="!isSign" @click="sign">
						签到
					</view>
					<view class="sign1" v-if="isSign">
						已签到
					</view>
				</view>
				<view class="vip-box">
					<view class="img-box">
						<image src="../../static/vip.png" mode="widthFix"></image>
					</view>
					<view style="margin-left: 15rpx;">會員書庫</view>
					<view style="margin-left: 30rpx;">開通VIP即可暢享閱讀VIP作品</view>
					<view class="right">
						<image src="../../static/right-white.png" mode="widthFix"></image>
					</view>
				</view>
				<view class="state">
					<view class="state-box" v-for="(item,index) in myInfo" :key="index" @click="goBookstore(index)">
						<view class="img-box">
							<image :src="item.icon" mode="widthFix"></image>
						</view>
						<view :style="'color: '+item.color+';'">
							{{item.content}}
						</view>
					</view>
				</view>
				<view class="fun-list">
					<view class="left">
						<view class="img-box">
							<image src="../../static/sign.png" mode="widthFix"></image>
						</view>
						<view style="color: #999999; margin-left: 30rpx;">
							签到记录
						</view>
						<view style="color: #999999; margin-left: 150rpx;" v-if="isLogin">
							已签到{{signArr.length}}天
						</view>
					</view>
					<view class="right">
						<image src="../../static/right.png" mode="widthFix"></image>
					</view>
				</view>
				<view class="fun-list" @click="goPersonal()">
					<view class="left">
						<view class="img-box">
							<image src="../../static/user-info.png" mode="widthFix"></image>
						</view>
						<view style="color: #999999; margin-left: 30rpx;">
							个人资料
						</view>
					</view>
					<view class="right">
						<image src="../../static/right.png" mode="widthFix"></image>
					</view>
				</view>
				<view class="fun-list">
					<view class="left">
						<view class="img-box">
							<image src="../../static/setting.png" mode="widthFix"></image>
						</view>
						<view style="color: #999999; margin-left: 30rpx;" @click="backMy()">
							退出个人中心
						</view>
					</view>
					<view class="right">
						<image src="../../static/right.png" mode="widthFix"></image>
					</view>
				</view>
				<!-- ------------------------------------------- -->
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name: 'my',
		data() {
			return {
				isLogin: false,
				userInfo: [],
				user: [],
				isRefresh: false,
				isSign: false,
				signArr: [],
				myInfo: [{
					count: 0,
					title: '书架',
					icon: '../../static/my-bookshelf.png',
					content: '我的书架',
					color: '#FD5E40'
				}, {
					count: 0,
					title: '评论数',
					icon: '../../static/my-comments.png',
					content: '我的评论',
					color: '#B0A7F0'
				}, {
					count: 0,
					title: '浏览数',
					icon: '../../static/my-record.png',
					content: '浏览记录',
					color: '#1497DB'
				}, ]
			};
		},
		methods: {
			goLogin() {
				uni.navigateTo({
					url: '../login/login'
				})
			},
			backMy() {
				let userInfo = uni.getStorageSync('userInfo');
				if (!userInfo) {
					uni.showToast({
						title: '未登录',
						icon: 'none'
					})
				} else {
					uni.showModal({
						title: '您确定退出个人中心吗?',
						success(res) {
							if (res.confirm) {
								uni.showToast({
									title: '退出成功',
									icon: 'success'
								})
								// uni.removeStorageSync('userInfo')
								localStorage.removeItem('userInfo')
								location.reload()
							} else if (res.cancel) {
								// uni.showToast({
								// 	title:'取消退出',
								// 	icon:'none'
								// })
							}
						}
					})
				}

			},
			getBookstore() {
				uniCloud.callFunction({
					name: 'getUser',
					data: {
						phone: this.userInfo.phone
					}
				}).then(res => {
					// console.log('res ==> ', res);
					this.user = res.result.data[0];
					// console.log('this.user ==> ', this.user);
					this.myInfo[0].count = this.user.bookstore.length
					this.myInfo[2].count = this.user.history.length
					this.signArr = res.result.data[0].sign
					// console.log('this.user ==> ',this.user);
					let date = new Date();
					let month = date.getMonth() + 1
					let day = date.getDate()
					let sign = month + '月' + day + '日'
					for (let i = 0; i < res.result.data[0].sign.length; i++) {
						if (res.result.data[0].sign[i] == sign) {
							this.isSign = true
						}
					}
					uni.hideLoading()
				}).catch(err => {
					console.log('err ==> ', err);
				})
			},
			goBookstore(index) {
				if (index == 0) {
					uni.switchTab({
						url: '../bookstore/bookstore'
					})
				} else if (index == 2) {
					uni.navigateTo({
						url: '../history/history'
					})
				}
			},
			sign() {
				let date = new Date();
				let month = date.getMonth() + 1
				let day = date.getDate()
				let sign = month + '月' + day + '日'
				let user = uni.getStorageSync('userInfo');
				if (!user) {
					uni.showToast({
						title:'还没登录',
						icon:'none'
					})
					return;
				}
				// console.log('sign ==> ',sign);
				// console.log('this.userInfo ==> ',this.userInfo);
				this.isSign = true;
				this.signArr.push(sign)
				// console.log('this.signArr ==> ',this.signArr);
				uniCloud.callFunction({
					name: 'addSign',
					data: {
						phone: this.userInfo.phone,
						sign: this.signArr
					}
				}).then(res => {
					console.log('res ==> ', res);
					uni.showToast({
						title: '签到成功',
						icon: "none"
					})
				}).catch(err => {
					console.log('err ==> ', err);
				})

			},
			goPersonal() {
				uni.navigateTo({
					url:'../personal/personal'
				})
			}
		},
		onShow() {
			let userInfo = uni.getStorageSync('userInfo');
			if (!userInfo) {
				return
			}
			uni.showLoading({
				title: '正在加载中...'
			});
			this.userInfo = JSON.parse(userInfo);
			// console.log(this.userInfo);
			if (userInfo) {
				this.isLogin = true;
			}
			this.getBookstore();
		},

	}
</script>

<style lang="scss">
	.bg {
		width: 100%;
		// height: 1234rpx;
		height: 100vh;
		background-image: url('../../static/background-image.png');
		background-repeat: no-repeat;
	}

	.top {
		width: 700rpx;
		height: 100rpx;
		margin: 0 auto;
		// background-color: red;
	}

	.user-list {
		width: 680rpx;
		height: 1100rpx;
		margin: 0 auto;
		background-color: #FCECCC;
		border-radius: 30rpx;
		border: 2rpx solid #FC768A;

		.user-list-top {
			height: 200rpx;
			// background-color: red;
			display: flex;
			align-items: center;

			.user-avatar {
				width: 110rpx;
				height: 110rpx;
				border-radius: 100rpx;
				margin-left: 30rpx;

				image {
					width: 100%;
					display: block;
				}
			}

			.user-id {
				margin-left: 30rpx;

				.user-name {
					margin-bottom: 20rpx;
					font-size: 32rpx;
					// font-weight: 550;
				}

				.id {
					font-size: 12px;
					color: #999999;
				}
			}

			.goLogin {
				font-weight: 700;
				font-size: 36rpx;
				width: 260rpx;
			}

			.user-info {
				width: 40rpx;
				height: 50rpx;
				// background-color: red;
				margin-left: 190rpx;

				image {
					width: 100%;
					display: block;
				}
			}
		}

		.my-state {
			display: flex;
			margin-left: 30rpx;
			height: 80rpx;
			align-items: center;

			.my-state-list {
				width: 100rpx;

				// background-color: red;
				text-align: center;

				.count {
					font-size: 36rpx;
					font-weight: 700;
				}

				.title {
					font-size: 26rpx;
				}
			}

			.sign {
				width: 120rpx;
				height: 60rpx;
				border-radius: 30rpx;
				background-color: #EB732A;
				text-align: center;
				line-height: 60rpx;
				color: white;
				font-size: 30rpx;
				margin-left: 200rpx;
			}

			.sign1 {
				width: 120rpx;
				height: 60rpx;
				border-radius: 30rpx;
				background-color: #ccc;
				text-align: center;
				line-height: 60rpx;
				color: #fff;
				font-size: 30rpx;
				margin-left: 200rpx;
			}
		}

		.vip-box {
			width: 100%;
			height: 100rpx;
			background-color: #C59C6E;
			margin-top: 50rpx;
			border-top-left-radius: 50rpx;
			border-top-right-radius: 50rpx;
			display: flex;
			align-items: center;
			font-size: 26rpx;
			color: white;

			.img-box {
				width: 50rpx;
				height: 50rpx;
				margin-left: 30rpx;

				image {
					width: 100%;
					display: block;
				}
			}

			.right {
				width: 40rpx;
				height: 38rpx;
				// background-color: red;
				margin-left: 30rpx;

				image {
					width: 100%;
					display: block;
				}
			}
		}

		.state {
			width: 600rpx;
			margin: 0 auto;
			height: 140rpx;
			// background-color: red;
			display: flex;
			justify-content: space-between;

			// border-bottom: 1px solid #FC768A;
			.state-box {
				width: 140rpx;
				height: 140rpx;
				// background-color: green;
				font-size: 28rpx;
				text-align: center;

				.img-box {
					width: 60rpx;
					height: 60rpx;
					margin: 16rpx auto;

					image {
						width: 100%;
						display: block;
					}
				}
			}
		}

		.fun-list {
			margin: 20rpx auto;
			width: 600rpx;
			height: 120rpx;
			// background-color: green;
			border-bottom: 1px solid #FC768A;
			display: flex;
			align-items: center;
			justify-content: space-between;

			.left {
				display: flex;
				align-items: center;

				.img-box {
					width: 70rpx;
					height: 70rpx;

					image {
						width: 100%;
						display: block;
					}
				}
			}

			.right {
				width: 40rpx;
				height: 38rpx;
				// background-color: red;
				// margin-left: 190rpx;

				image {
					width: 100%;
					display: block;
				}
			}
		}
	}
</style>